﻿/*
Copyright © 2011 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.IO;

namespace Simple.Storage.Serializer
{
    /// <summary>
    /// Provides the serialization logic associated with <see cref="byte"/> instances.
    /// </summary>
    public class ByteSerializer : AbstractMonoTypeSerializer
    {
        /// <summary>
        /// Gets the <see cref="byte"/> type.
        /// </summary>
        protected override Type AssociatedType
        {
            get { return typeof(byte); }
        }

        /// <summary>
        /// Serializes a <see cref="byte"/> value.
        /// </summary>
        /// <param name="writer">The target writer.</param>
        /// <param name="value">The serialized value.</param>
        protected override void InternalSerialize(BinaryWriter writer, object value)
        {
            writer.Write((byte)value);
        }

        /// <summary>
        /// Reads a <see cref="byte"/> value.
        /// </summary>
        /// <param name="reader">The source reader.</param>
        /// <returns>The deserialized value.</returns>
        protected override object InternalDeserialize(BinaryReader reader)
        {
            return reader.ReadByte();
        }
    }
}
